/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controlador;

import Facade.MaquinariaFacade;
import VO.MaquinariaVO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import vista.MaquinariaBean;

/**
 *
 * @author Jorge
 */
public class modificarMaquinaria2Action extends DispatchAction {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String LOAD = "load";
     private final static String CANCEL = "cancel";
     private final static String ERROR="error";
    
    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward seleccionar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MaquinariaFacade fcdMaquinaria = new MaquinariaFacade();
        MaquinariaVO updtMaquinaria;
        String maqSeleccionada = request.getParameter("cmbNomenclatura").toString();
        updtMaquinaria = fcdMaquinaria.cargarMaquinaria(maqSeleccionada);

       System.out.println(updtMaquinaria.getMarca());

         if (isCancelled(request))
        {
             return mapping.findForward(CANCEL);
        }

        request.setAttribute("datosMaquina", updtMaquinaria);

        return mapping.findForward(LOAD);
    }


     public ActionForward volver(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

         return mapping.findForward(CANCEL);
     }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward actualizar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        MaquinariaBean bean = (MaquinariaBean)form;
        MaquinariaVO editada;

        MaquinariaFacade fcd= new MaquinariaFacade();
        bean.setAnio(request.getParameter("anio"));
        bean.setCapEstanque(request.getParameter("capEstanque"));
        bean.setCapPersonas(request.getParameter("capPersonas"));
        bean.setMarca(request.getParameter("marca"));
        bean.setModelo(request.getParameter("modelo"));
        bean.setNombre(request.getParameter("nombre"));
        bean.setNomenclatura(request.getParameter("nomenclatura"));
        bean.setPaisOrigen(request.getParameter("paisOrigen"));
        bean.setPotenciaBomba(request.getParameter("potenciaBomba"));
        bean.setTipoMaquina(request.getParameter("tipoMaquina"));

        
        if (isCancelled(request))
        {
             return mapping.findForward(CANCEL);
        }   


        if(bean.getAnio().isEmpty() |bean.getNombre().isEmpty() | bean.getNomenclatura().isEmpty() | 
           bean.getCapEstanque().isEmpty() | bean.getCapPersonas().isEmpty() | bean.getMarca().isEmpty() |
           bean.getModelo().isEmpty()| bean.getPaisOrigen().isEmpty() | bean.getTipoMaquina().isEmpty() | bean.getPotenciaBomba().isEmpty())
        {
            bean.setAviso("<span style='color:red'>Ingrese todos los campos</span>");
            request.setAttribute("datosMaquina", bean);
            
            return mapping.findForward(ERROR);
        }

        editada = new MaquinariaVO();
        editada.setAnio(bean.getAnio());
        editada.setCapEstanque(bean.getCapEstanque());
        editada.setCapPersonas(bean.getCapPersonas());
        editada.setMarca(bean.getMarca());
        editada.setModelo(bean.getModelo());
        editada.setNomenclatura(bean.getNomenclatura());
        editada.setNombre(bean.getNombre());
        editada.setPaisOrigen(bean.getPaisOrigen());
        editada.setPotenciaBomba(bean.getPotenciaBomba());
        editada.setTipoMaquina(bean.getTipoMaquina());

        if(fcd.editarMaquinaria(editada))
        {
        bean.setAviso("<span style='color:blue'>Actualizacion exitosa! </span>");
        request.setAttribute("datosMaquina", bean);
        return mapping.findForward(SUCCESS);
        }else {
        bean.setAviso("<span style='color:red'>Ha ocurrido un error</span>");
            request.setAttribute("datosMaquina", bean);

            return mapping.findForward(ERROR);
        }
    }
}